﻿
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
''' <summary>
''' Programmed by: Muhammad Yasin
''' Email: Muhammad-Yasin@outlook.com
''' </summary>
''' <remarks></remarks>
<DefaultEvent("CheckedChanged"), DefaultProperty("Checked")>
Public Class TickBox
    Inherits Control

    Public Sub New()
        Me.Width = 24
        Me.Height = 24
        Me.AccessibleRole = Windows.Forms.AccessibleRole.CheckButton
        InitializeComponent()
    End Sub

    Dim BoderStyle As ButtonBorderStyle = ButtonBorderStyle.Solid
    <Category("Appearance"), DefaultValue(GetType(ButtonBorderStyle), "Solid"), Description _
    ("Gets or sets the appearance of the Control")>
    Public Property BorderStyle() As ButtonBorderStyle
        Get
            Return BoderStyle
        End Get
        Set(value As ButtonBorderStyle)
            BoderStyle = value
            Me.Invalidate()
        End Set
    End Property
    Dim Hoover As Boolean = False
    <Browsable(False), Description("Gets or sets the appearance of the Control")>
    Public Property Hover() As Boolean
        Get
            Return Hoover
        End Get
        Set(value As Boolean)
            Hoover = value
            Me.Invalidate()
        End Set
    End Property

    Private isChecked As Boolean = True
    <Category("Appearance"), DefaultValue(True), Description _
   ("Gets or sets the checked state of the control")>
    Public Property Checked() As Boolean
        Get
            Return isChecked
        End Get
        Set(value As Boolean)
            isChecked = value
            Me.Invalidate()
        End Set
    End Property
    Private TikColor As Color = Color.Black
    <Category("Appearance"), DefaultValue(True), Description _
   ("Gets or sets the The Color of the tick")>
    Public Property TickColor() As Color
        Get
            Return TikColor
        End Get
        Set(value As Color)
            TikColor = value
            Me.Invalidate()
        End Set
    End Property
    Dim LightColor As New Color
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim state As ButtonState
        MyBase.OnPaint(e)

        If Me.Checked Then
            state = state Or ButtonState.Checked
        End If
        LightColor = ControlPaint.Light(TickColor)

        If Me.Checked = True Then
            Dim path As New GraphicsPath()
            Dim points() As Point = { _
                New Point(10 * (Me.Width / 32), 27 * (Me.Height / 32)), _
                New Point(2 * (Me.Width / 32), 17 * (Me.Height / 32)), _
                New Point(5 * (Me.Width / 32), 13 * (Me.Height / 32)), _
                New Point(11 * (Me.Width / 32), 20 * (Me.Height / 32)), _
                New Point(27 * (Me.Width / 32), 6 * (Me.Height / 32)), _
                New Point(29 * (Me.Width / 32), 9 * (Me.Height / 32))}

            path.AddLines(points)
            Dim aBrush As System.Drawing.SolidBrush
            aBrush = New System.Drawing.SolidBrush(TikColor)
            Dim surface As Graphics = Me.CreateGraphics
            surface.SmoothingMode = SmoothingMode.AntiAlias
            surface.FillPath(aBrush, path)

            ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, TickColor, BorderStyle)
            If Hover Then
                Me.BackColor = LightColor
            Else
                Me.BackColor = Color.White
            End If
        Else
            ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, TickColor, BorderStyle)
            If Hover Then

                Me.BackColor = LightColor
            Else
                Me.BackColor = Color.White
            End If
        End If

    End Sub

    Private Sub TickBox_MouseEnter(sender As Object, e As EventArgs) Handles Me.MouseEnter
        Hover = True
    End Sub

    Private Sub TickBox_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
        Hover = True
    End Sub

    Private Sub TickBox_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
        Hover = False
    End Sub

    Private Sub TickBox_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        Me.Checked = Not Me.Checked
        OnCheckedChanged(New EventArgs())
    End Sub
    Private Sub CheckBoxSizable_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Me.Invalidate()
    End Sub

    Public Event CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Protected Sub OnCheckedChanged(ByVal e As EventArgs)
        RaiseEvent CheckedChanged(Me, e)
    End Sub

    <Browsable(False)>
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(value As String)
            MyBase.Text = value
        End Set
    End Property
    ''' <summary>
    ''' Show the about box of the control.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ShowAbout()
        MsgBox("Programmed by: Muhammad Yasin" + vbCrLf + "Email: Muhammad-Yasin@outlook.com", MsgBoxStyle.Information, "About Smooth Label")
    End Sub
End Class
